package com.office.meeting.manage.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.office.common.utils.SecurityUtils;
import com.office.meeting.invited.pojo.entity.ConferenceInvitationEntity;
import com.office.schedule.manage.domain.Schedule;
import com.office.schedule.manage.mapper.ScheduleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.office.meeting.manage.mapper.ConferenceInvitationMapper;
import com.office.meeting.manage.domain.ConferenceInvitation;
import com.office.meeting.manage.service.IConferenceInvitationService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 我的会议管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-16
 */
@Service
public class ConferenceInvitationServiceImpl implements IConferenceInvitationService {

    @Autowired
    private ConferenceInvitationMapper conferenceInvitationMapper;

    @Resource
    private ScheduleMapper scheduleMapper;

    /**
     * 查询我的会议管理列表
     *
     * @param conferenceInvitation 我的会议管理
     * @return 我的会议管理
     */
    @Override
    public List<ConferenceInvitation> selectConferenceInvitationList(ConferenceInvitation conferenceInvitation) {
        // 获取当前登录用户的id
        Long userId = SecurityUtils.getUserId();
        return conferenceInvitationMapper.selectConferenceInvitationList(conferenceInvitation, userId);
    }

    /**
     * 修改为同意
     *
     * @param ids 选中的会议的id
     * @return 返回修改后的结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateConferenceInvitation(List<Long> ids) {
        // 根据会议的id查询会议信息
        List<ConferenceInvitationEntity> invitations = conferenceInvitationMapper.selectinvitationsInfo(ids);
        // 向日程表添加数据
        List<Schedule> list = new ArrayList<>();
        for (int i = 0; i < invitations.size(); i++) {
            Schedule schedule = new Schedule();
            // 设置日程表和会议关联的id
            schedule.setMeetingId(ids.get(i));
            // 设置日程类型为会议
            schedule.setDatesType(0);
            // 获取会议时间
            Date startTime = invitations.get(i).getStartTime();
            // 设置会议开始时间
            schedule.setStartTime(startTime);
            // 设置会议结束时间设置
            schedule.setEndTime(startTime);
            // 设置日程标题
            String meetingName = invitations.get(i).getMeetingName();
            // 截取最后两个字符看是不是 会议 如果不是拼接一个 会议
            meetingName = meetingName.lastIndexOf("会议") == -1 ? meetingName + " 会议" : meetingName;
            schedule.setTitle(meetingName);
            // 设置日程描述
            schedule.setDescription("密码" + invitations.get(i).getPassword());
            // 设置关联用户的id
            schedule.setUserId(invitations.get(i).getUserId());
            list.add(schedule);
        }
        // 向日程表插入会议日程
        scheduleMapper.insertScheduleLists(list);
        // 查询数据
        return conferenceInvitationMapper.updateConferenceInvitation(ids);
    }


    /**
     * @param ids    会议的id
     * @param reason 拒绝的理由
     * @return 影响的行数
     */
    @Override
    public Integer updateConferenceInvitationByIds(Long[] ids, String reason) {
        return conferenceInvitationMapper.updateReasonConferenceInvitation(ids, reason);
    }

    /**
     * @param id 根据id查询会议
     * @return 返回会议信息
     */
    @Override
    public ConferenceInvitation seletcMeetingById(Long id) {
        return conferenceInvitationMapper.getMeetingById(id);
    }
}
